<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置并连接数据库
$dbConfig = include('db_config.php');
$dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
$pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// 获取文件标题参数
$fileTitle = isset($_GET['file_title']) ? trim($_GET['file_title']) : null;

try {
    // 检查是否为空
    if (empty($fileTitle)) {
        throw new Exception("文件标题参数为空！");
    }

    // 执行模糊查询
    $stmt = $pdo->prepare("SELECT * FROM mi_file WHERE 文件标题 LIKE ?");
    $stmt->execute(["%{$fileTitle}%"]);

    // 获取查询结果
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 返回结果
    if (empty($results)) {
        throw new Exception("空");
    }

    echo json_encode(['status' => 'success', 'message' => '成功', 'data' => $results], JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    $errorResult = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($errorResult, JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
    $errorResult = [
        'status' => 'error',
        'message' => $e->getMessage()
    ];
    echo json_encode($errorResult, JSON_UNESCAPED_UNICODE);
}
